<template>
  <el-dialog
    :title="dialogObj.title"
    append-to-body
    :visible.sync="dialogObj.show"
    :close-on-click-modal="false"
    width="600px"
  >
    <el-form
      ref="form"
      status-icon
      :model="form"
      label-width="140px"
      :rules="rules"
      v-if="dialogObj.show"
    >
      <el-form-item label="活动" prop="activityId">
        <el-select
          v-model="form.activityId"
          placeholder="请选择活动"
          value-key="index"
          size="mini"
          @change="selectActivityOption"
          :disabled="!!dialogObj.id"
        >
          <el-option
            v-for="(data, index) in ActivityList"
            :key="index"
            :label="data.activityName"
            :value="data.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item
        label="请输入银盒子ID"
        prop="mchId"
        v-if="roundPayType === 0"
      >
        <el-input
          v-model="form.mchId"
          style="width:200px;"
          size="mini"
          placeholder="请输入银盒子ID"
        ></el-input>
      </el-form-item>
      <el-form-item label="银盒子ID" prop="mchIdII" v-if="roundPayType === 1">
        <el-input
          v-model="form.mchIdII"
          style="width:200px;"
          size="mini"
          placeholder="请输入银盒子ID"
          @change="changeInput"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="sub" size="mini">确 定</el-button>
      <el-button @click="dialogObj.show = false" size="mini">取 消</el-button>
    </span>
  </el-dialog>
</template>
<script>
import {
  validateTemplateID,
  validateTemplateActivity,
  validateTemplateIDII
} from "@u/validate";
import { _debounce } from "@u/debounce";
import { getActivityApi, updatePaySettingApi, getProportionApi } from "@s/api";
export default {
  name: "dialogCom",
  props: ["dialogObj"],
  data() {
    return {
      roundPayType: 1,
      ActivityList: [],
      form: {
        payType: 2, // 2 活动
        mchId: "",
        activityId: "",
        activityName: "",
        activityDate: "",
        activityDates: "",
        activityEndDate: "",
        mchIdII: ""
      },
      rules: {
        mchId: [{ trigger: "blur" }, { validator: validateTemplateID }],
        mchIdII: [
          {
            trigger: "blur",
            message: "银盒子ID为纯数字"
          },
          { validator: validateTemplateIDII }
        ],
        activityId: [
          { required: true, message: "请选择活动", trigger: "blur" },
          { validator: validateTemplateActivity }
        ]
      }
    };
  },
  methods: {
    initDialog() {
      if (this.dialogObj.id) {
        this.form.mchId = this.dialogObj.form.mchId;
        this.form.mchIdII = this.dialogObj.form.mchId;
        this.form.activityId = this.dialogObj.form.activityId;
        this.form.activityName = this.dialogObj.form.activityName;
        this.form.activityDate = this.dialogObj.form.activityDate;
        this.form.activityDates = this.dialogObj.form.activityDates;
        this.form.activityEndDate = this.dialogObj.form.activityEndDate;
      } else {
        this.form.mchId = "";
        this.form.mchIdII = "";
        this.form.activityId = "";
        this.form.activityName = "";
        this.form.activityDate = "";
        this.form.activityDates = "";
        this.form.activityEndDate = "";
      }
    },
    // 活动信息
    getActivityList() {
      getActivityApi().then(res => {
        // 所有活动用户
        this.ActivityList = res;
        // 判断回显的数据是否存在列表中
        if (this.dialogObj.id) {
          let findId = this.ActivityList.filter(item => {
            return item.id == this.form.activityId;
          });
          if (findId.length == 0) {
            let newArr = {
              activityName: this.form.activityName,
              id: this.form.activityId
            };
            this.ActivityList.push(newArr);
          }
        }
      });
    },
    // 监听全额付款时输入值
    changeInput(val) {
      this.form.mchId = val;
    },
    // 活动选择
    selectActivityOption(val) {
      this.form.activityId = val;
      this.ActivityList.forEach(item => {
        if (val == item.id) {
          this.form.activityName = item.activityName;
          this.form.activityDate = item.activityStartDate;
          this.form.activityDates = JSON.stringify(item.activityDates);
          this.form.activityEndDate = item.activityEndDate;
        }
      });
    },
    sub: _debounce(function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.dialogObj.id) {
            this.updateSub();
          } else {
            this.addSub();
          }
        }
      });
    }),
    addSub() {
      let json = JSON.parse(JSON.stringify(this.form));
      delete json.mchIdII;
      let jsonArr = [json];
      updatePaySettingApi(jsonArr).then(() => {
        this.dialogObj.show = false;
        this.$emit("reflash");
        this.$message({
          type: "success",
          message: "添加成功！"
        });
      });
    },
    updateSub() {
      let json = JSON.parse(JSON.stringify(this.form));
      delete json.mchIdII;
      json.id = this.dialogObj.form.id;
      let jsonArr = [json];
      updatePaySettingApi(jsonArr).then(() => {
        this.dialogObj.show = false;
        this.$emit("reflash");
        this.$message({
          type: "success",
          message: "修改成功！"
        });
      });
    },
    getproportionList() {
      getProportionApi({ limit: 10, page: 1 }).then(res => {
        if (res.list.length > 0) {
          this.roundPayType = res.list[0].depositPercent;
        }
      });
    }
  },
  watch: {
    "dialogObj.show"(val) {
      if (val) {
        this.initDialog();
        this.getActivityList();
        this.getproportionList();
      }
    }
  }
};
</script>
<style scoped lang="scss"></style>
